/**
 * fileName:  NutrientReader.java
 * author:  Lei
 * Company:  Cloudland Studio
 * Createdate:  2014-6-3 下午10:54:39
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-6-3          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.store.read;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.cloudland.commercial.masterdata.bean.NutritionType;
import org.cloudland.commercial.masterdata.store.DatabaseReader;
import org.cloudland.commercial.masterdata.store.entity.NutritionEntity;
import org.cloudland.commercial.masterdata.store.entity.PageQueryEntity;
import org.cloudland.jdbc.ibatis.masterdata.entity.NutritionDictEntity;
import org.cloudland.jdbc.ibatis.masterdata.handler.NutritionDictMapper;
import org.cloudland.jdbc.ibatis.masterdata.handler.condition.NutritionTerm;
import org.springframework.stereotype.Service;

 /**
 * <pre> 
 * 营养信息获取
 * </pre>
 * @ClassName  NutrientReader
 * @author  Lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
@Service
public class NutrientReader implements DatabaseReader<NutritionEntity> {
	
	@Resource(name = "nutritionDictMapper")
	private NutritionDictMapper nutritionMapper;

	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 *   
	 */
	public NutrientReader() {
		// TODO Auto-generated constructor stub
	}
	
	private NutritionType doCheckType (String typeCode) {
		
		NutritionType type;
		if (NutritionType.MINERAL.code().equals(typeCode)) {
			type = NutritionType.MINERAL;
		} else if (NutritionType.NUTRIENT.code().equals(typeCode)) {
			type = NutritionType.NUTRIENT;
		} else {
			type = NutritionType.VITAMI;
		}
		
		return type;
	}
	
	/**
	 * <pre>
	 * 
	 * </pre>
	 *
	 * @param terms
	 * @return   
	 */
	private PageQueryEntity<NutritionEntity> findEfficacyByTerm(Map<String, String> terms) {
		
		NutritionTerm term = new NutritionTerm();
		term.setName(terms.get("name"));
		term.setBeginRow(Integer.parseInt(terms.get("startNo")));
		term.setPageRow(Integer.parseInt(terms.get("pageSize")));
		term.setType(terms.get("type"));
		
		List<NutritionDictEntity> listDao = nutritionMapper.findPageByTerm(term);
		List<NutritionEntity> list = new ArrayList<NutritionEntity>(listDao.size());
		for (NutritionDictEntity entityDao : listDao) {
			list.add(new NutritionEntity(entityDao.getId(), entityDao.getName(), entityDao.getDescribe(), (int) entityDao.getSort(), doCheckType(entityDao.getType())));
		}
		int count = nutritionMapper.findCountByTerm(term);
		
		return new PageQueryEntity<NutritionEntity>(count, list);
	}
	
	/**
	 * <pre>
	 * 
	 * </pre>
	 *
	 * @param id
	 * @return   
	 */
	public NutritionEntity findNutrientById(String id) {
		
		NutritionDictEntity entityDao = nutritionMapper.findById(id);
		
		NutritionType type = doCheckType(entityDao.getType());
		NutritionEntity entity = new NutritionEntity(entityDao.getId(), entityDao.getName(), entityDao.getDescribe(), (int) entityDao.getSort(), type);
		return entity;
	}
	
	/**
	 * <pre>
	 * 
	 * </pre>
	 *
	 * @param name
	 * @param startNo
	 * @param pageSize
	 * @return   
	 */
	public PageQueryEntity<NutritionEntity> findNutrientByName(String name, String type, Integer startNo, Integer pageSize) {
		
		Map<String, String> terms = new HashMap<String, String>();
		terms.put("name", name);
		terms.put("type", type);
		terms.put("startNo", String.valueOf(startNo));
		terms.put("pageSize", String.valueOf(pageSize));
		
		return this.findEfficacyByTerm(terms);
	}
	
	/**
	 * <pre>
	 * 
	 * </pre>
	 *
	 * @param startNo
	 * @param pageSize
	 * @return   
	 */
	public PageQueryEntity<NutritionEntity> findNutrientForPage(String type, Integer startNo, Integer pageSize) {
		
		Map<String, String> terms = new HashMap<String, String>();
		terms.put("type", type);
		terms.put("startNo", String.valueOf(startNo));
		terms.put("pageSize", String.valueOf(pageSize));
		
		return this.findEfficacyByTerm(terms);
	}
	
	/**
	 * <pre>
	 * 查询全部
	 * </pre>
	 *
	 * @return
	 */
	public List<NutritionEntity> findAllNutrition(String type)
	{
		List<NutritionDictEntity> daoList =  nutritionMapper.findAll(type);
		if(null == daoList)
		{
			 return null;
		}
		List<NutritionEntity> list = new ArrayList<NutritionEntity>();
		for(NutritionDictEntity daoEntity : daoList)
		{
			list.add(new NutritionEntity(daoEntity.getId(),daoEntity.getName(),daoEntity.getDescribe(),(int)daoEntity.getSort(),doCheckType(daoEntity.getType())));
		}
		return list;
	}

}
